package Data::Form::Field::Boolean;
use strict;
use Carp;

use base qw(Data::Form::Field::Base);
our @attributes = qw(  boolean );

__PACKAGE__->mk_accessors( @attributes );

our $VERSION = '0.01';

sub new {
	my $class		= shift;
	my %params = @_;
	# We should put a check in here for 'required', 'multiple'
	my $obj		= $class->SUPER::new( %params );
	$obj->_boolean( $params{boolean} );
	$obj->value( $obj->boolean->{ $obj->default }  ); 
	return $obj
}
	

sub fill_in {
    my $self = shift;
    my $value = shift || '';

    croak "$self is a readonly field" if $self->readonly;
	
	$self->value( $value eq $self->boolean->{true} ? $self->boolean->{true} : $self->boolean->{false} );
    $self->validate;
}

sub _boolean {
	my $self 	   = shift;
	my $boolean = shift;
	
	if ( not ( defined($boolean->{true})  and defined( $boolean->{false}))){
		croak q{'boolean' parameter to 'new' must be of the form {true => $true_value, false => $false_value} };
	}
	
	$self->boolean( $boolean )
}

1;